﻿using System;
using Coproject.ViewModels;
using NLog;
using NLog.Config;
using NLog.Targets;
using nlog = NLog;

namespace Caliburn.Micro.Logging.NLog
{
    public class ConfigureNLog
    {
        private static IEventAggregator events;

        public ConfigureNLog()
        { }

        public static void InitializeNLog()
        {
            MethodCallTarget target = new MethodCallTarget();
            target.ClassName = typeof(ShellViewModel).AssemblyQualifiedName;
            target.MethodName = "LogMethod";
            target.Parameters.Add(new MethodCallParameter("${level}"));
            target.Parameters.Add(new MethodCallParameter("${message}"));

            SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

#if false

            SimpleConfigurator.ConfigureForTargetLogging(
                new FileTarget()
                {
                    FileName = @"${specialfolder:MyDocuments}/CoProjectLog.${shortdate}.txt",
                    Layout = new CsvLayout()
                    {
                        Columns =
                                {
                                    new CsvColumn("Time", "${longdate}"),
                                    new CsvColumn("Level", "${level}"),
                                    new CsvColumn("Lessage", "${message}"),
                                    new CsvColumn("Logger", "${logger}"),
                                },
                    }
                },
                LogLevel.Debug);

        public static void LogMethod(string level, string message)
        {
            events = Caliburn.Micro.IoC.Get<IEventAggregator>();

            //Console.WriteLine("l: {0} m: {1}", level, message);
            //System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
            //IdeaBlade.Core.TraceMessage tm = new IdeaBlade.Core.TraceMessage(null, message, true);

            //var tp = IdeaBlade.Core.TracePublisher.LocalInstance;
            //tp.Publish(tm);
            if (events != null)
            {
                var m = new NlogMessage(message);
                events.Publish(m);
            }

        }

#endif
        }



    }

    public class NlogMessage
    {
        public NlogMessage(String message)
        {
            Message = message;
        }
        public string Message { get; set; }
    }

    public class NLogLogger : ILog, ILogExtended
    {
        #region Fields
        private readonly nlog.Logger _innerLogger;
        #endregion

        #region Constructors

        public NLogLogger(Type type)
        {
            _innerLogger = nlog.LogManager.GetLogger(type.Name);
        }

        #endregion

        #region ILog Members
        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        public void Error(Exception exception)
        {
            _innerLogger.ErrorException(exception.Message, exception);
        }
        /// <summary>
        /// Logs the message as info.
        /// </summary>
        /// <param name="format">A formatted message.</param><param name="args">Parameters to be injected into the formatted message.</param>
        public void Info(string format, params object[] args)
        {
            _innerLogger.Info(format, args);
        }
        /// <summary>
        /// Logs the message as a warning.
        /// </summary>
        /// <param name="format">A formatted message.</param><param name="args">Parameters to be injected into the formatted message.</param>
        public void Warn(string format, params object[] args)
        {
            _innerLogger.Warn(format, args);
        }
        #endregion
        #region Implementation of ILogExtended
        /// <summary>
        /// Logs the message as error.
        /// </summary>
        /// <param name="format">A formatted message.</param>
        /// <param name="args">Parameters to be injected into the formatted message.</param>
        public void Error(string format, params object[] args)
        {
            _innerLogger.Error(format, args);
        }
        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <param name="format">A formatted message.</param>
        /// <param name="args">Parameters to be injected into the formatted message.</param>
        public void Error(Exception exception, string format, params object[] args)
        {
            _innerLogger.ErrorException(string.Format(format, args), exception);
        }
        #endregion
    }
}
